package com.hardware.service.impl;

import com.hardware.dll.JinglunDLL;
import com.hardware.entity.IdCard;
import com.hardware.entity.Respond;
import com.hardware.service.JinglunService;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

/**
 * 参考网址: https://blog.csdn.net/hanwenyi520/article/details/88712772
 * 精伦读卡器
 * @作者 韩宗森
 * @创建时间 2024年10月24日 15:50
 */
@Service
public class JinglunServiceImpl implements JinglunService {

    // 获取 精伦读卡器-iDR210 身份证信息
    @Override
    public Respond jinglunIdCard() {

        Respond respond = new Respond();

        int iPort = 1001; // iPort：1001  USB串口
        // 读卡器端口初始化
        int ret = JinglunDLL.INSTANCE.InitComm(iPort);

        if (ret != 1) {
            respond.setStatus(0);
            respond.setMsg("没有找到读卡器!");
        } else {
            System.out.println("读卡器连接成功!");

            // 读取身份证信息
            respond = getCardInfos();

        }
        // 关闭 读卡器
        ret = JinglunDLL.INSTANCE.CloseComm();
        if (ret != 1) {
            System.out.println("读卡器关闭连接错误! 错误码: " + ret);
        } else {
            System.out.println("读卡器关闭连接成功!");
            System.out.println("---------------------------------------------------------------------");
        }

        return respond;
    }


    /**
     * 读取身份证信息
     * @return
     */
    private Respond getCardInfos() {

        Respond respond = new Respond();

        // 卡认证接口 数用于发现身份证卡并选择卡  1:正确 0 错误
        int ret = JinglunDLL.INSTANCE.Authenticate();

        if (ret != 1) {
            respond.setStatus(0);
            respond.setMsg("读卡失败！请将身份证重新放读卡器上！");
        } else {
            byte[] Name = new byte[31];
            byte[] Gender = new byte[3];
            byte[] Folk = new byte[10];
            byte[] BirthDay = new byte[9];
            byte[] Code = new byte[19];
            byte[] Address = new byte[71];
            byte[] Agency = new byte[31];
            byte[] ExpireStart = new byte[9];
            byte[] ExpireEnd = new byte[9];
            byte[] directory = new byte[200];; //设置照片存放目录

            // 本函数用于读取卡中基本信息，包括文字信息与图像信息。文字信息以字符串格式输出
            ret = JinglunDLL.INSTANCE.ReadBaseInfosPhoto(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd, directory);
            if (ret > 0) {
                respond.setStatus(1);
                respond.setMsg("信息读取成功！");
                System.out.println("信息读取成功!" + ret);
                try {
                    System.out.println("-----------------------------------------------");
                    // 把身份证信息封装到 类
                    IdCard idCard = new IdCard();
                    idCard.setName(new String(Name, "GBK").trim());
                    idCard.setGender(new String(Gender, "GBK").trim());
                    idCard.setFolk(new String(Folk, "GBK").trim());
                    idCard.setBirthDay(new String(BirthDay, "GBK").trim());
                    idCard.setCode(new String(Code, "GBK").trim());
                    idCard.setAddress(new String(Address, "GBK").trim());
                    idCard.setAgency(new String(Agency, "GBK").trim());
                    idCard.setExpireStart(new String(ExpireStart, "GBK").trim());
                    idCard.setExpireEnd(new String(ExpireEnd, "GBK").trim());
                    idCard.setDirectory(new String(directory, "GBK").trim());

                    // 头像图片
                    Path path = Paths.get(idCard.getDirectory() + ".\\photo.jpg");
                    byte[] imageBytes = Files.readAllBytes(path);
                    String photo = Base64.getEncoder().encodeToString(imageBytes);
                    idCard.setPhoto(photo);

                    respond.setData(idCard);

                } catch (UnsupportedEncodingException e) {
                    respond.setStatus(0);
                    respond.setMsg("卡信息异常！");
                    e.printStackTrace();
                } catch (IOException e) {
                    respond.setStatus(0);
                    respond.setMsg("头像图片异常！");
                    e.printStackTrace();
                }
            }
        }
        System.out.println(respond);
        return respond;
    }


}
